import config from '../config'

const tips = {
    1: '抱歉，出现了一个错误',
    1005: '不正确的开发者key',
    3000: '该期内容不存在'
}

export default class HTTP {
    request(params) {
        let { url, data, method = 'GET', success } = params;
        return new Promise((resolve, reject)=>{
	        wx.request({
		        url: config.api_base_url + url,
		        method,
		        data,
		        header: {
			        'content-type': 'application/json',
			        'appkey': config.appkey
		        },
		        success: (res)=> {
			        let code = res.statusCode.toString();
			        if (code.startsWith('2')) {
			            resolve(res.data);
			        } else {
				        let error_code = res.data.error_code;
				        this._show_error(error_code);
				        reject();
			        }
		        },
		        fail: (err)=> {
			        this._show_error();
			        reject();
		        }
	        });
        })
    }
    _show_error(error_code = 1) {
        wx.showToast({
            title: tips[error_code],
            icon: 'none',
            duration: 2000
        })
    }
}